// 像素字符串专成正数的数字
const pxStrToNumber = (str) => {
  const match = str.match(/-?(\d+)px/);
  if (match) {
    return Math.abs(parseInt(match[1], 10));
  }
  return 0;
}

// 将返回的数据处理成base64 
export function getBase64Img(params = {}) {
  const {
    width,
    height,
    y,
    x,
    soureBuffer
  } = params;
  const iconWidth = pxStrToNumber(width);
  const iconHeight = pxStrToNumber(height);
  const cropY = pxStrToNumber(y);
  const cropX = x || 0;
  return sharp(soureBuffer).extract({ width: iconWidth, height: iconHeight, left: cropX, top: cropY }).toFormat('png').toBuffer();
}

// 获取地址栏关键字参数
export function getUrlParams(url) {
  const match = url.match(/([^\/]+)\.html$/);
  if (match) {
    const extractedValue = match[1];
    return extractedValue
  } else {
    return false
  }
}